#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/4/25 15:18
# @Author  : 王凯
# @File    : parser.py
# @Project : scrapy_spider
import datetime
import json
import re

import parsel
from loguru import logger

from components.config import WFQ_SOURCE_MYSQL_CONFIG
from utils.db.mysqldb import MysqlDB

wfq_source_db = MysqlDB(
    ip=WFQ_SOURCE_MYSQL_CONFIG["MYSQL_IP"],
    port=WFQ_SOURCE_MYSQL_CONFIG["MYSQL_PORT"],
    db=WFQ_SOURCE_MYSQL_CONFIG["MYSQL_DB"],
    user_name=WFQ_SOURCE_MYSQL_CONFIG["MYSQL_USER_NAME"],
    user_pass=WFQ_SOURCE_MYSQL_CONFIG["MYSQL_USER_PASS"],
)


class Parser:
    def get_raw_data(self, html):
        response = parsel.Selector(html)
        raw_data = response.re_first(r"window\.rawData\s*=\s*(\{.*\});")
        if not raw_data:
            logger.error("未获取到数据")
            return False
        raw_data = json.loads(raw_data)
        return raw_data

    def get_mall_goods(self, info_dict, mall_id):
        """
        店铺名称
        店铺ID
        平台名称

        商品ID
        商品名称
        商品价格

        月销量
        月销售

        一级类目
        二级类目
        三级类目

        排名

        :param info_dict:
        :return:
        """
        res = []
        goods_list = info_dict.get("goods_list") or []
        for goods in goods_list:
            goods_id = goods.get("goods_id")
            goods_name = goods.get("goods_name")
            price = goods.get("price")
            cnt = goods.get("cnt")
            sales_tip = goods.get("sales_tip")
            if sales_tip:
                num = re.findall(r"(\d+)", sales_tip)
                if num:
                    cnt = num[0]
            item = {
                "mall_id": mall_id,
                "goods_id": goods_id,
                "goods_name": goods_name,
                "price": price,
                "sale_num": cnt,
                "update_date": datetime.datetime.now().strftime("%Y-%m-%d"),
            }
            res.append(item)
        if res:
            wfq_source_db.add_batch_smart(
                "net_pdd_goods_detail", res, update_columns=["mall_id", "goods_id", "goods_name", "price", "sale_num"]
            )
            self.save_activate_mall_id(mall_id)
        if info_dict.get("has_more") is False:
            self.save_is_all(mall_id)

    def get_cat_level(self, info_dict, mall_id):
        category_list = info_dict.get("category_list")
        # all_good_count = [i for i in category_list if i['name'] == '全部商品'][0].get("goods_count")

        has_level_flag = [i for i in category_list if i.get("level_flag")]
        for level_flag in has_level_flag:
            item = {
                "goods_id": level_flag["goods_id"],
                "mall_id": mall_id,
                "update_date": datetime.datetime.now().strftime("%Y-%m-%d"),
            }
            if level_flag.get("category_list"):
                for one_goods in level_flag.get("category_list"):
                    if level_flag["level_flag"] == 2:
                        item = {
                            "level_2": one_goods["name"],
                            "goods_id": one_goods["goods_id"],
                            "mall_id": mall_id,
                            "update_date": datetime.datetime.now().strftime("%Y-%m-%d"),
                        }
            else:
                # 一级
                if level_flag["level_flag"] == 1:
                    item = {
                        "level_1": level_flag["name"],
                        "goods_id": level_flag["goods_id"],
                        "mall_id": mall_id,
                        "update_date": datetime.datetime.now().strftime("%Y-%m-%d"),
                    }
            wfq_source_db.add_smart(
                "net_pdd_goods_detail", item, update_columns=["mall_id", "goods_id", "level_1", "level_2", "level_3"]
            )

    def run_one(self, mall_id):
        logger.info(f"{mall_id} 解析开始")
        # is_not_exist = wfq_source_db.find(f"select * from net_pdd_shop_info where mall_id = '{mall_id}' and is_all is null", to_json=True)
        is_not_exist = True
        if is_not_exist:
            datas = wfq_source_db.find(f"select * from net_pdd_proxy_log where mall_id = '{mall_id}'", to_json=True)
            for data in datas:
                if data["response"]:
                    if "/turing/mall/query_mall_category_list" in data.get("base_url"):
                        # self.get_cat_level(data["response"], mall_id)
                        pass
                    else:
                        self.get_mall_goods(data["response"], mall_id)
        logger.info(f"{mall_id} 解析完成")

    def save_activate_mall_id(self, mall_id):
        """
        保存商品信息
        :param mall_id:
        :return:
        """
        shop_info = wfq_source_db.find(f"select * from net_pdd_shop_info where mall_id = '{mall_id}'", to_json=True)
        if shop_info:
            wfq_source_db.add_smart(
                "net_pdd_shop_info",
                {**shop_info[0], "shop_status": "开店"},
                update_columns=["shop_status"],
            )

    def save_is_all(self, mall_id):
        shop_info = wfq_source_db.find(f"select * from net_pdd_shop_info where mall_id = '{mall_id}'", to_json=True)
        if shop_info:
            wfq_source_db.add_smart(
                "net_pdd_shop_info",
                {**shop_info[0], "shop_status": "开店", "is_all": 1},
                update_columns=["is_all"],
            )


if __name__ == "__main__":
    import concurrent.futures

    # ids = wfq_source_db.find("select distinct mall_id as t_id from net_pdd_proxy_log", to_json=True)
    ids = wfq_source_db.find(
        "select distinct mall_id as t_id from net_pdd_shop_info where is_all is null and shop_status = '开店'",
        to_json=True,
    )
    with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
        executor.map(Parser().run_one, [i["t_id"] for i in ids])

    # Parser().run_one(404700629)
    # Parser().run_one(408087627)
